package com.gg.user.gguser.service.impl;

import com.gg.oss.alioss;
import com.gg.user.gguser.service.GgFileService;
import com.gg.util.SnowFlake;
import com.gg.vo.return_format;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author yangxinlei
 * @date 2020/4/18
 */
@Service
public class GgFileServiceImpl implements GgFileService {


    @Override
    public Object upload(String filename, byte[] data) {
        try {
            FileOutputStream fos = new FileOutputStream("./" + filename);
            fos.write(data);
            fos.close();
            System.out.println("-------文件上传成功！-------------");
            //上传oss nana生成
            SnowFlake snowFlake = new SnowFlake(2, 3);
            String fileNameUpdate = String.valueOf(snowFlake.nextId());
            alioss.putAliBaBaOss("./" + filename,fileNameUpdate + ".jpg","yyburst");
            String url = "https://yyburst.oss-cn-hangzhou.aliyuncs.com/" + fileNameUpdate + ".jpg";
            File fileDalect = new File("./" + filename);
            fileDalect.delete();
            return return_format.return_format_All(0,url,"");
        } catch (IOException e) {
            return return_format.return_format_error(0,"");
        }
    }
}
